﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;

namespace ConsoleApp1.Requests
{
    class CheckWebCookie
    {
		public static void MakeRequests(string ck,string phone)
		{
			HttpWebResponse response;
			string responseText;

			if (Request_l_huya_com(ck, phone,out response))
			{
				responseText = ReadResponse(response);
				//responseText = System.Web.HttpUtility.JavaScriptStringEncode(responseText);
                if (responseText.Contains("bindMobile"))
                {
                    Console.WriteLine($"{phone},成功：{responseText}");
                }
                else
				{
					Console.WriteLine($"{phone},无效:{responseText}");
				}
				response.Close();
			}
		}

		private static string ReadResponse(HttpWebResponse response)
		{
			using (Stream responseStream = response.GetResponseStream())
			{
				Stream streamToRead = responseStream;
				if (response.ContentEncoding.ToLower().Contains("gzip"))
				{
					streamToRead = new GZipStream(streamToRead, CompressionMode.Decompress);
				}
				else if (response.ContentEncoding.ToLower().Contains("deflate"))
				{
					streamToRead = new DeflateStream(streamToRead, CompressionMode.Decompress);
				}

				using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.UTF8))
				{
					return streamReader.ReadToEnd();
				}
			}
		}

		private static bool Request_l_huya_com(string ck, string phone,out HttpWebResponse response)
		{
			response = null;

			try
			{
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://l.huya.com/udb_web/udbport2.php?m=HuyaOutside&do=isBindHuyaMobile&callback=jQuery17205873706021523957_1605063168934&_=1605063169156");

				request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 Edg/86.0.622.63";
				request.Accept = "*/*";
				request.Headers.Add("Sec-Fetch-Site", @"same-site");
				request.Headers.Add("Sec-Fetch-Mode", @"no-cors");
				request.Headers.Add("Sec-Fetch-Dest", @"script");
				request.Referer = "https://i.huya.com/";
				request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
				request.Headers.Set(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
				request.Headers.Set(HttpRequestHeader.Cookie, ck);
				//huya_web_rep_cnt=38;first_username_flag=hy_98637385_first_1;undefined=undefined;huya_flash_rep_cnt=8;h_unt=1605012450;_yasids=__rootsid%3DC921AE4C01600001663A13D040E98740;__yaoldyyuid=1199560196743;rep_cnt=1;huyaweb_rep_cnt=2;_rep_cnt=3;__yamid_new=C921AE3C8B0000013CB61707106010F9;username=hy_98637385;udb_uid=1199560196743;udb_status=1;udb_passport=hy_98637385;udb_origin=1;huyawap_rep_cnt=10;udb_biztoken=AQBKVMpk8KE5sDlRXEJgJ_QsMLMHIr2AG5xtGU8J1TxzPsA51-EGScBTDoeK6zs2tw518xq3yKzhGIHNfbibtToEAKECpc_OYWdLPu-icHbYcD2vUINzivNZukb6H33tFNr75rA05B9iMbBgw9RPED4C57s-0b5jcFgM0gQUhR90BKvcPrDKTHkNPX2wv8ncFivM0nsdKqn3pfTjkuMb0pXxEmZMB4JmXtZfVfybzbvXS1GidwLoYFjoNXlhRIVUHYlczmjGPyC-t4vN6cOG91QKUt7CRc08tf9BGAwM5Y6jhAG1LFj2aM9Nx_2G3rA1SUaCCaW1s4kgCfZjIWgEfuhV;SoundValue=0.50;Hm_lvt_51700b6c722f5bb4cf39906a596ea41f=1605012347;isInLiveRoom=;web_qrlogin_confirm_id=01b849b6-e42b-4412-9f71-ec28bd0b8461;udb_version=2.4;Hm_lpvt_51700b6c722f5bb4cf39906a596ea41f=1605012482;PHPSESSID=i58bbjvb1aoi5tku3qttp54rh0;yyuid=1199560196743;udb_accdata=08615916775037;__yasmid=0.625432937850906;__yamid_tt1=0.625432937850906;udb_guiddata=34cfb31939c9435d931b038eec1b03bc;udb_passdata=3;");
				//request.Headers.Set(HttpRequestHeader.ProxyAuthorization, "Basic SFo3ODMxMTlaNzg2WDFVRDpCRTM1QjExQzE5MzJCNkND");

				response = (HttpWebResponse)request.GetResponse();
			}
			catch (WebException e)
			{
				if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
				else return false;
			}
			catch (Exception)
			{
				if (response != null) response.Close();
				return false;
			}

			return true;
		}
	}
}
